#!/bin/bash
 # 服务名称
 SERVER_NAME=$1
 # 镜像名称
 IMAGE_NAME=$2
 echo "------ 开始构建镜像：${SERVER_NAME} ------"
 docker build -t ${IMAGE_NAME} .
 echo "------ 镜像构建结束：${IMAGE_NAME} ------"

 echo "------ 删除容器：$SERVER_NAME ------"
 docker rm -f $SERVER_NAME

 echo "------ 开始运行容器：$SERVER_NAME ------"
 docker run -d --name $SERVER_NAME --restart always -p 9999:9999 ${IMAGE_NAME}
 echo "------ 清理虚悬镜像 ------"
 if [[ -n $(docker images | grep "none" | awk '{print $3}') ]];then
     docker rmi -f $(docker images | grep "none" | awk '{print $3}')
 fi
